package com.haha.helloworld.datastorage;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.haha.helloworld.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

public class FileActivity extends AppCompatActivity {
    private EditText et_name;
    private Button btn_save;
    private Button btn_show;
    private TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
        et_name=(EditText)findViewById(R.id.et_name);
        btn_save=(Button)findViewById(R.id.btn_save);
        btn_show=(Button)findViewById(R.id.btn_show);
        tv_content=(TextView)findViewById(R.id.tv_content);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    save(et_name.getText().toString().trim());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    tv_content.setText(read());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    //存储数据
    private void save(String content) throws IOException {
        FileOutputStream fileOutputStream=null;
        try {
            //设置写入的文件路径，以及模式
//            fileOutputStream=openFileOutput("haha.txt",MODE_PRIVATE);
            //新建文件夹
            File fileDir=new File(Environment.getExternalStorageDirectory(),"pengdata/haha/yes");
            if (!fileDir.isFile()){
                fileDir.mkdirs();
            }
            //创建文件，文件不存在就新建一个
            File file = new File(fileDir,"haha.txt");
            if (!file.exists()){
                file.createNewFile();
            }
            fileOutputStream=new FileOutputStream(file);
            fileOutputStream.write(content.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileOutputStream.close();
        }
    }
    //读取数据
    public String read() throws IOException {
        FileInputStream fileInputStream=null;
        try {
            //设置读取的文件路径
//            fileInputStream=openFileInput("haha.txt");
            File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                    +"/pengdata/haha/yes","haha.txt");
            fileInputStream = new FileInputStream(file);
            //设置缓冲区
            byte[] off=new byte[1024];
            StringBuilder stringBuilder = new StringBuilder("");
            int len=0;
            while ((len=(fileInputStream.read(off)))>0){
                //根据起始位置拼接字符串
                stringBuilder.append(new String(off,0,len));
            }
            return stringBuilder.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileInputStream.close();
        }
        return null;
    }
}